<?php
namespace User\Controller;
use Think\Controller;
/*
 * 移动端人员查询入口控制器 IndexController.class.php
 */
class IndexController extends CommonController {
    /* 属性声明 */
    private $uuid = null;
    private $corpid = null;
    //用户主配置文件
    private $main_conf_file = null;
    //用户顶级子集排序文件
    private $top_sort_conf_file = null;
    //原始顶级子集文件
    private $ori_top_file = "./Org/Data/Original_top_list.json";
    
    /* 方法声明 */
    public function __construct() {
        parent::__construct();
        
        /**  本地调试区域开始  **/
//        session('uuid','9987654321');
//        session('corpid','wx354598b372f7261a');
        /**  本地调试区域结束  **/
        
        //获取员工唯一id标识 uuid
        if(!$this -> uuid = session('uuid')){
            //通过网页授权获取成员uuid
            $this -> uuid = $this -> auth();
        }
        //dump($this->uuid);exit;
        $this -> corpid =  session('corpid');
        //初始化全局属性
        $this -> main_conf_file = "./Org/Data/UserData/main_conf/{$this->corpid}_main.conf";
        $this -> top_sort_conf_file = "./Org/Data/UserData/top_sort/".$this -> corpid.'.conf';
        //生成微信jssdk配置信息
        if(!$signPackage=session('signPackage')){
            $token = getToken($this->corpid,$this->secret);
            $this -> jssdk($this->corpid,$this->secret,$token);
            session('signPackage',$this -> signPackage);
            $this -> assign('signPackage',$this -> signPackage);
        }else{
            $this -> assign('signPackage',$signPackage);
        }
        //获取员工数据
        A('Pc/Common')->getAwsUuidinfo($this->uuid);
    }
    
    /**
     * 通过网页授权获取企业号信息 成员唯一标识 uuid
     */
    private function auth(){
        $data = I('get.');
        if((!$code=$data['code']) || (!$corpid=$data['state'])){
            $this -> error('未经授权访问!');
        }
        
        $this -> corpid = $corpid;
        
        session('corpid',$this->corpid);
        $this -> code = $code;
        $secret = $this -> company_list[$corpid]['secret'];
        //生成企业号 token 信息
        $token = getToken($corpid,$secret);

        //生成微信jssdk配置信息
        $token = getToken($this->corpid,$this->secret);
        $this -> jssdk($this->corpid,$this->secret,$token);
        session('signPackage',$this -> signPackage);
        $this -> assign('signPackage',$this -> signPackage);

        //通过code获取成员唯一标识 uuid
        return $this -> getUuid($token);
    }
    
    /**
     * 通过userid获取企业号 成员uuid
     * @param string $token 获取到的凭证
     * @return string $uuid 返回成员唯一标识uuid
     */
    private function getUuid($token){
        $userid = $this -> getUserid($this->code,$token);
        $request_url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={$token}&userid={$userid}";
        $user_info = json_decode(https_curl($request_url),true);
        //获取uuid
        $user_info = $user_info['extattr']['attrs'];
        $column_list = array_column($user_info,'name');
        //初始化uuid
        $uuid = null;
        foreach($column_list as $k => $v){
            if($v=='uuid'){
                $uuid = $user_info[$k]['value'];
                break;
            }
        }
        if(!$uuid){
            $this -> error('未匹配到您的员工标识id');
        }
        session('uuid',$uuid);
        return $uuid;
    }
    
    /**
     * 通过code获取成员userid
     */
    private function getUserid($code,$token){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$token}&code={$code}";
        $result = json_decode(https_curl($url),true);
        if(!$userid = $result['UserId']){
            $this -> error('非法code参数!');
        }
        return $userid;
    }
    
    /**
     * 员工自助入口方法
     */
    public function index(){
        //获取首页导航数据
        $menu_list = $this -> getMenuList();
        //添加导航图片数据
        $menu_list = $this -> addThumb($menu_list);
        //检测是否提交过审核信息
        if($this -> redis -> hexists('user_dealt',$this->uuid)){
            $dealt = 1;
        }else{
            $dealt = 0;
        }
        /***************追加子集是否更改数据*****************/
        $this -> redis -> select(10);
        $userinfo = json_decode($this->redis->hget('user_list',$this->uuid),true)['information'];
        //dump($userinfo);exit;
        foreach($menu_list as $k=>&$v){
            $v['change_type'] = $userinfo[$v['name']]['change_type'];
        }
        //dump($menu_list);exit;
        $this -> assign('menu',$menu_list);
        $this -> assign('uuid',$this->uuid);
        $this -> assign('corpid',$this->corpid);
        $this -> assign('dealt',$dealt);
        $this -> display('index');
    }
    
    /**
     * 获取首页导航子集数据
     */
    private function getMenuList(){
        //检测是否存在自定义顶级子集排序显示配置文件
        if(file_exists($this->top_sort_conf_file)){
            //初始化数据缓冲区
            $buffer_list = 
            unserialize(file_get_contents($this->top_sort_conf_file));
        }else{
            //不存配置文件
            $buffer_list = file_get_contents($this->ori_top_file);
            $buffer_list = json_decode($buffer_list,true);
            //批量添加显示状态数据
            foreach($buffer_list as $k => $v){
                $buffer_list[$k]['display'] = true;
            }
        }
        return $buffer_list;
    }
    
    /**
     * 添加图片缩略图数据
     */
    public function addThumb($menu_list){
        $offset = 1;
        foreach($menu_list as $k => $v){
            $menu_list[$k]['thumb'] = "/Public/User/image/{$offset}.png";
            $offset++;
        }
        return $menu_list;
    }
}